
#Construtor for the Cell object. The cells parent is the environment that declared it. 
#The gridloc is its position in the parents cell list.
setConstructorS3("ACell", function(parent = "", gridloc = "") {
	# Extends RAgent object
	extend(RAgent(), "ACell",
		.gridloc = gridloc, 
		.parent = parent
	
	)
})
#Method used to populate the cell with the desired agents. Each new agent should be called to initiate its own
#data
 setMethodS3("populate", "ACell", function(this, agents) {
    #Tell user that agents are being added to the cell 
	print(paste("Adding agents to cell ", this$.gridloc[1], ",", this$.gridloc[2], ":"))
	#foor each agent in the list match and create
	for(i in 1:length(agents)){
	  if(agents[i] == "SnowCrab"){
	    print("Adding snowcrab") #Tell user that snowcrab are being added
		#Create a snowcrab object with its homecell equal to this cell and
		#a reference to its position in this cells agent list. May not need this reference
		#in the future as it may be able to reference itself with somthing like li[["SnowCrab"]]
		newSC = ASnowCrab(homecell = this, lispos = length(this$.li)+1)
		newSC$createIndividuals() #Call to the snowcrab objects create individuals based on rules in ASnowCrab.r
		this$.li[["SnowCrab"]] =  newSC #Store the new SnowCrab object. May change to be able to add multiple 
		                                #snowcrab objects at this [["SnowCrab"]] location
	 
	  }
	}
 })	
 
 #Any send commands issued to this cell will be carried out here. This keeps track of the 
 #message and a direct reference to who sent it (fro). More messages handeling can be added 
 setMethodS3("do", "ACell", function(this, mes, fro) {
    #If the message is plusDay the the environment is telling the cell that a day has passed
	#And it needs to do its daily routines. This should only happen if it has not already 
	#been told that a day has passed. It checks this with this$.parent$.updated != this$.updated 
	#if true it changes its .updated variable and can no longer be entered untill another day hs passed
	if(mes == "plusDay" && this$.parent$.updated != this$.updated){
	    this$.updated = this$.updated * -1 #Change to tell that this cell was visited today
	    this$.dayspassed = this$.dayspassed + 1 #track dayspassed
		#May wish to tell user that this cell knows a day has passed, was used for testing may be usefull later 
		#print(paste("CELL", this$.gridloc[1], " ", this$.gridloc[2], " UPDATING DAY")) 
		
		this$.liindex = this$.liindex + 1 #Move li index to 1 to start visiting agents to tell them that a day has passed
        #While loop that visits each agent
		while(!is.null(this$.li[this$.liindex][[1]])){		
			#Send the message that a day has passed to each agent		
			this$send("plusDay", this$.li[this$.liindex][[1]])
			this$.liindex = this$.liindex + 1 #Move index to next agent
		}
		this$.liindex = 0 #Set index back to zero soo next day can start back at the first
	}
	
	
	
})